package com.pt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pt.dto.request.CommentRequest;
import com.pt.entity.Comment;
import com.pt.entity.CommentLike;
import com.pt.mapper.CommentLikeMapper;
import com.pt.mapper.CommentMapper;
import com.pt.service.CommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentLikeMapper commentLikeMapper;

    @Override
    public Page<Comment> getTorrentComments(Long torrentId, int page, int size) {
        return lambdaQuery()
                .eq(Comment::getTorrentId, torrentId)
                .eq(Comment::getIsDeleted, false)
                .orderByDesc(Comment::getCreatedTime)
                .page(new Page<>(page, size));
    }

    @Override
    @Transactional
    public Comment addComment(CommentRequest request, Long userId) {
        Comment comment = new Comment();
        comment.setTorrentId(request.getTorrentId());
        comment.setUserId(userId);
        comment.setContent(request.getContent());
        comment.setParentId(request.getParentId());
        comment.setLikes(0);
        comment.setCreatedTime(LocalDateTime.now());
        comment.setIsDeleted(false);

        save(comment);
        return comment;
    }

    @Override
    @Transactional
    public void likeComment(Long commentId, Long userId) {
        // 检查是否已经点赞
        boolean exists = commentLikeMapper.selectCount(
                new LambdaQueryWrapper<CommentLike>()
                        .eq(CommentLike::getCommentId, commentId)
                        .eq(CommentLike::getUserId, userId)
        ) > 0;

        if (!exists) {
            // 添加点赞记录
            CommentLike commentLike = new CommentLike();
            commentLike.setCommentId(commentId);
            commentLike.setUserId(userId);
            commentLike.setCreatedTime(LocalDateTime.now());
            commentLikeMapper.insert(commentLike);

            // 更新评论点赞数
            baseMapper.incrementLikeCount(commentId);
        }
    }

    @Override
    @Transactional
    public void unlikeComment(Long commentId, Long userId) {
        // 删除点赞记录
        commentLikeMapper.delete(
                new LambdaQueryWrapper<CommentLike>()
                        .eq(CommentLike::getCommentId, commentId)
                        .eq(CommentLike::getUserId, userId)
        );

        // 更新评论点赞数
        baseMapper.decrementLikeCount(commentId);
    }

    @Override
    @Transactional
    public void deleteComment(Long commentId, Long userId) {
        Comment comment = getById(commentId);
        if (comment == null || comment.getIsDeleted()) {
            throw new RuntimeException("评论不存在");
        }

        // 只有评论作者或管理员可以删除评论
        if (!comment.getUserId().equals(userId)) {
            // 这里应该检查用户是否为管理员，简化处理
            throw new RuntimeException("没有权限删除该评论");
        }

        // 软删除评论
        lambdaUpdate()
                .eq(Comment::getId, commentId)
                .set(Comment::getIsDeleted, true)
                .update();
    }
} 